package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelCommend;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.mapper.TravelCommendMapper;
import cn.wolfcode.trip.base.mapper.TravelContentMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.query.UserProfilesQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import javafx.scene.chart.PieChart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class TravelServiceImpl implements ITravelService {

    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelContentMapper travelContentMapper;

    public Travel get(Long id) {
        TravelContent travelContent = travelContentMapper.selectByPrimaryKey(id);
        Travel travel = travelMapper.selectByPrimaryKey(id);
        if (travelContent != null) {
            travel.setTravelContent(travelContent);
        }
        return travel;
    }

    public void savaOrUpdate(Travel travel) {
        Date date = new Date();
        travel.setLastUpdateTime(date); // 最后创建时间
        if (travel.getId() != null) {
            // 更新游记
            travelMapper.updateByPrimaryKey(travel);
            //更新游记的内容
            TravelContent travelContent = travel.getTravelContent();
            travelContent.setId(travel.getId()); // 游记的id
            travelContentMapper.updateByPrimaryKey(travelContent);
        } else {
            // 保存游记
            travel.setAuthor(UserContext.getUser()); // 设置作者
            travel.setCreateTime(date); // 设置创建的时间
            travelMapper.insert(travel);

            // 保存游记的内容
            TravelContent travelContent = travel.getTravelContent();
            travelContent.setId(travel.getId()); // 游记的id
            travelContentMapper.insert(travelContent);
        }
    }

    public void changeState(Long id, Integer state) {
        travelMapper.changeStateById(id, state);
    }

    public PageInfo queryforTravels(UserProfilesQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Travel> travels = travelMapper.selectTravelsByUserId(qo);
        return new PageInfo(travels);
    }

    public  Map<String,Object> getCounts(String keyword) {
        return travelMapper.getCounts(keyword);
    }

    public PageInfo<Travel> query(TravelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Travel> list = travelMapper.selectForList(qo);
        return new PageInfo<Travel>(list);
    }
}
